#!/usr/bin/env zsh
# ==============================================
#                 基础环境配置
# ==============================================

# --------------------------
# 语言与编辑器设置
# --------------------------
export LANG=zh_CN.UTF-8
export LC_ALL=zh_CN.UTF-8
export EDITOR=vim
export VISUAL=vim

# --------------------------
# 路径配置
# --------------------------
export PATH="$HOME/bin:/usr/local/bin:$HOME/.local/bin:$HOME/.cargo/bin:$PATH"

# Flatpak 支持
export XDG_DATA_DIRS="/var/lib/flatpak/exports/share:/home/hdh/.local/share/flatpak/exports/share:$XDG_DATA_DIRS"

# ==============================================
#               Oh My Zsh 配置
# ==============================================
export ZSH="$HOME/.oh-my-zsh"
export ZSH_CUSTOM="$HOME/.local/omz"

# --------------------------
# 主题配置
# --------------------------
ZSH_THEME="random"
ZSH_THEME_RANDOM_CANDIDATES=(
    "robbyrussell" 
    "agnoster"
    "frisk"
    "ys"
    "bira"
)

# --------------------------
# 插件配置（分类整理）
# --------------------------
plugins=(
    # 核心工具
    git
    sudo
    extract
    z
    vi-mode
    
    # 界面增强
    fzf
    fzf-tab
    zsh-autosuggestions
    zsh-syntax-highlighting
    
    # 开发辅助
    vscode
    sublime
    command-not-found
    colored-man-pages
    
    # 实用功能
    alias-finder
    rand-quote
    history
    copypath
)

# --------------------------
# Oh My Zsh 行为配置
# --------------------------
HYPHEN_INSENSITIVE="true"          # 忽略补全连字符差异
ENABLE_CORRECTION="true"           # 命令自动校正
COMPLETION_WAITING_DOTS="true"     # 补全等待提示
HIST_STAMPS="yyyy-mm-dd"           # 历史时间格式
zstyle ':omz:update' mode reminder # 更新提醒模式
zstyle ':omz:update' frequency 10  # 每10天检查更新

# 加载 Oh My Zsh
source $ZSH/oh-my-zsh.sh

# ==============================================
#               Shell 行为配置
# ==============================================

# --------------------------
# 历史记录优化
# --------------------------
HISTFILE=~/.zsh_history
HISTSIZE=100000
SAVEHIST=100000
setopt appendhistory            # 追加而非覆盖历史
setopt extended_history        # 记录时间戳
setopt hist_expire_dups_first  # 淘汰重复历史
setopt hist_ignore_space       # 忽略空格开头的命令
setopt share_history           # 共享历史记录

# --------------------------
# 交互行为
# --------------------------
setopt autocd                  # 目录名直接跳转
setopt extendedglob            # 扩展通配符
setopt interactivecomments     # 允许交互式注释
setopt nocaseglob              # 不区分大小写匹配
unsetopt beep                  # 禁用错误提示音

# --------------------------
# 补全系统
# --------------------------
autoload -Uz compinit
compinit -d ~/.cache/zsh/zcompdump-$ZSH_VERSION
zstyle ':completion:*' menu select
zstyle ':completion:*' matcher-list 'm:{a-z}={A-Z}'
zstyle ':completion:*' list-colors "${(s.:.)LS_COLORS}"
zstyle ':completion:*' rehash true

# ==============================================
#                 别名系统
# ==============================================

# --------------------------
# 文件操作
# --------------------------
alias ls='ls --color=auto --group-directories-first'
alias ll='ls -lh'
alias la='ls -A'
alias l='ls -CF'
alias cp='cp -iv'
alias mv='mv -iv'
alias rm='rm -Iv'
alias df='df -h'
alias du='du -h'

# --------------------------
# 开发工具
# --------------------------
alias gcc='gcc -Wall -Wextra'
alias g++='g++ -Wall -Wextra'
alias make='make -j$(nproc)'
alias vim='vim -p'              # 多文件标签页
alias vi='vim'

# --------------------------
# 压缩/解压
# --------------------------
alias tarc='tar -czvf'
alias tarx='tar -xzvf'
alias zip='zip -r'
alias unzip='unzip -O GBK'      # 处理中文编码

# --------------------------
# 网络相关
# --------------------------
alias ip='ip -4 -br -c a'
alias ports='ss -tulanp'
alias ping='ping -c 5'
alias wget='wget -c'            # 断点续传

# --------------------------
# Git 快捷方式
# --------------------------
alias gs='git status'
alias ga='git add'
alias gc='git commit'
alias gd='git diff'
alias gp='git push'
alias gl='git pull'
alias gco='git checkout'
alias gb='git branch'

# --------------------------
# 文件后缀关联
# --------------------------
alias -s {rb,py,js,c,java,txt,md}=vim
alias -s html=mate

# ==============================================
#                 增强功能
# ==============================================

# --------------------------
# FZF 配置
# --------------------------
export FZF_BASE="$HOME/.local/fzf"
[ -f ~/.local/.fzf.zsh ] && source ~/.local/.fzf.zsh
alias fzf="fzf --preview 'bat --style=numbers --color=always {} 2>/dev/null || cat {}'"

# --------------------------
# 实用函数
# --------------------------

# 创建并进入目录
mkcd() { mkdir -p "$1" && cd "$1" || return }

# 查找进程
psg() { ps aux | grep -v grep | grep -i "$@" }

# 统计代码行数
cloc() { 
    find . -name "*.$1" | xargs wc -l | sort -nr 
}

# ==============================================
#                 使用建议
# ==============================================

# 1. 性能优化建议：
#    - 若启动慢可添加 `zmodload zsh/zprof` 在文件开头
#    - 在文件末尾添加 `zprof` 分析启动耗时

# 2. 推荐安装工具：
#    - bat (彩色cat):    sudo apt install bat
#    - exa (现代ls):     sudo apt install exa
#    - ripgrep (快速grep): sudo apt install ripgrep
#    - delta (更好的diff):  sudo apt install git-delta

# 3. 主题推荐：
#    - powerlevel10k: 最快的主题框架
#      git clone --depth=1 https://github.com/romkatv/powerlevel10k.git ${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}/themes/powerlevel10k

# 4. 插件管理建议：
#    - 使用 zinit 替代原生插件管理可获得更快速度
#    - 减少插件数量可显著提高启动速度